<?php
class Reservation extends AppModel {
    public $name         = 'Reservation';
    public $displayField = 'total_amount';
    public $hasMany = array(
        'Stay' => array(
            'className' => 'Stay',
            'dependent' => true
        ),
        'ReservationField' => array(
            'className' => 'ReservationField',
            'dependent' => true
        )
    );

    public function reserveList ($params) {
        $page = $params['page'];
        $cond = 'r.`status_id`!="-1" ';

        if ($params['query'] && $params['qtype']) {
            $cond .= 'AND r.`'.$params['qtype'].'` LIKE "%'.$params['query'].'%" ';
        }

        $sql = 'SELECT COUNT(*) AS total FROM `reservations` AS r ' .
               'WHERE '.$cond;
        $ttl = $this->query($sql);

        $total = (count($ttl) == 0) ? 0 : $ttl[0][0]['total'];
        if ($total == 0) {
            return array(
                'page'  => 1,
                'total' => 0,
                'rows'  => array()
            );
        }

        $rpp   = isset($params['rp']) ? $params['rp'] : 10;
        $start = (($page-1) * $rpp);
        $sort  = empty($params['sortname']) ? 'id' : $params['sortname'];
        $ord   = (empty($params['sortorder']) || $params['sortorder'] == 'asc') ? 'ASC' : 'DESC';
        $sql   = 'SELECT r.*, g.`firstname`, g.`lastname`, g.`city`, g.`state`, s.`name` FROM `reservations` AS r ' .
                 'INNER JOIN `guests` as g ' .
                 'ON r.`guest_id`=g.`id` ' .
                 'INNER JOIN `reserve_statuses` as s ' .
                 'ON r.`status_id`=s.`id` ' .
                 'WHERE '.$cond .
                 'ORDER BY r.`'.$sort.'` '.$ord.' ' .
                 'LIMIT '.$start.', '.$rpp;
        $data = $this->query($sql);
        $rec  = array();

        if (count($data) == 0) {
            return array(
                'page'  => 1,
                'total' => 0,
                'rows'  => array()
            );
        }

        foreach ($data as $rs) {
            $id = $rs['r']['id'];

            $fullname = $rs['g']['firstname'].' '.$rs['g']['lastname'];
            $location = $rs['g']['city'].', '.$rs['g']['state'];
            $checkIn  = $this->serverToClientTime($rs['r']['check-in_date'], 'd-m-Y');
            $created  = $this->serverToClientTime($rs['r']['created'], 'd/m/Y H:i a');
            $modified = $this->serverToClientTime($rs['r']['modified'], 'd/m/Y H:i a');
            $actions  = '<div class="checkIn" onclick="reservation.checkin('.$id.');"></div>' .
                        '<div class="editRecord" onclick="reservation.edit('.$id.');"></div>';

            $cell = array(
                $id, $fullname, $location, $rs['s']['name'], $checkIn, $rs['r']['room_type'], $rs['r']['stay_count'],
                $rs['r']['notes'], $rs['r']['deposit'], $rs['r']['arrival_time'], $created, $modified, $actions
            );

            $rec[] = array(
                'id'   => $id,
                'cell' => $cell
            );
        }

        return array(
            'page'  => $page,
            'total' => $total,
            'rows'  => $rec
        );
    } // reserveList
} // Reservation
?>